package com.zb.filters;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Component
public class FeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        //获取请求对象
        HttpServletRequest request = servletRequestAttributes.getRequest();
        //请求头中的集合数据
        Enumeration<String> headerNames = request.getHeaderNames();
        //遍历集合
        while (headerNames.hasMoreElements()) {
            //获取请求头的key
            String key = headerNames.nextElement();
            //value
            String value = request.getHeader(key);
            System.out.println(key + ":" + value);
            //存储到下一次的请求头对象中
            requestTemplate.header(key, value);
        }

    }
}
